package com.lightboat.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lightboat.common.constant.CacheConstants;
import com.lightboat.common.utils.DateUtils;
import com.lightboat.common.utils.RedisUtils;
import com.lightboat.system.domain.CrmCity;
import com.lightboat.system.domain.vo.CrmCityTreeVo;
import com.lightboat.system.mapper.CrmCityMapper;
import com.lightboat.system.service.CrmCityAsyncService;
import com.lightboat.system.service.ICrmCityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 城市Service业务层处理
 *
 * @author lightboat
 * @date 2025-08-21
 */
@Service
@RequiredArgsConstructor
public class CrmCityServiceImpl extends ServiceImpl<CrmCityMapper, CrmCity> implements ICrmCityService {

    private final CrmCityMapper crmCityMapper;
    private final CrmCityAsyncService crmCityAsyncService;

    /**
     * 查询城市
     *
     * @param id 城市主键
     * @return 城市
     */
    @Override
    public CrmCity selectCrmCityById(Long id) {
        return crmCityMapper.selectById(id);
    }

    /**
     * 查询城市列表
     *
     * @param crmCity 城市
     * @return 城市
     */
    @Override
    public List<CrmCity> selectCrmCityList(CrmCity crmCity) {
        return crmCityMapper.selectCrmCityList(crmCity);
    }

    /**
     * 新增城市
     *
     * @param crmCity 城市
     * @return 结果
     */
    @Override
    public int insertCrmCity(CrmCity crmCity) {
        crmCity.setCreateTime(DateUtils.getNowDate());
        int result = baseMapper.insert(crmCity);
        if (result > 0) {
            crmCityAsyncService.async(crmCity.getParentId());
        }
        return result;
    }

    /**
     * 修改城市
     *
     * @param crmCity 城市
     * @return 结果
     */
    @Override
    public int updateCrmCity(CrmCity crmCity) {
        crmCity.setUpdateTime(DateUtils.getNowDate());
        int result = baseMapper.updateById(crmCity);
        if (result > 0) {
            crmCityAsyncService.async(crmCity.getParentId());
        }
        return result;
    }

    /**
     * 批量删除城市
     *
     * @param ids 需要删除的城市主键
     * @return 结果
     */
    @Override
    public int deleteCrmCityByIds(Long[] ids) {
        int result = baseMapper.deleteCrmCityByIds(ids);
        if (result > 0) {
            crmCityAsyncService.async(ids);
        }
        return result;
    }

    /**
     * 删除城市信息
     *
     * @param id 城市主键
     * @return 结果
     */
    @Override
    public int deleteCrmCityById(Long id) {
        return crmCityMapper.deleteCrmCityById(id);
    }

    /**
     * 获取城市树
     *
     * @return
     */
    @Override
    public List<CrmCityTreeVo> getListTree() {
        List<CrmCityTreeVo> cityList = RedisUtils.builder().getList(CacheConstants.CITY_LIST_TREE, CrmCityTreeVo.class);
        if (CollUtil.isEmpty(cityList)) {
            crmCityAsyncService.setListTree();
        }
        return RedisUtils.builder().getList(CacheConstants.CITY_LIST_TREE, CrmCityTreeVo.class);
    }
}
